import xlsxwriter

# 新建文件及sheet
workbook = xlsxwriter.Workbook('Expenses02.xlsx')
worksheet = workbook.add_worksheet()

# Add a bold format to use to highlight cells. 设置粗体，默认是False
bold = workbook.add_format({'bold': True})

# Add a number format for cells with money. 定义数字格式
money = workbook.add_format({'num_format': '$#,##0'})

# Write some data headers.自定义带粗体bold格式写表头
worksheet.write('A1', 'Item', bold)
worksheet.write('B1', 'Cost', bold)

# Some data we want to write to the worksheet.要写入工作表的数据
expenses = (['Rent', 1000], ['Gas', 100], ['Food', 300], ['Gym', 50])

# Start from the first cell below the headers.从标题下面第一个单元格开始。
row = 1
col = 0

# Iterate over the data and write it out row bu row.遍历数据并逐行写入
for item, cost in expenses:
    worksheet.write(row, col, item)
    worksheet.write(row, col+1, cost, money)
    row += 1

# Write a total using a formula.写入合计公式。
worksheet.write(row, 0, 'Total:', bold)
worksheet.write(row, 1, '=SUM(B2:B5)', money)

workbook.close()
